<template>
    <div class="sjplatformDetailForm">
        <div class="title-div">基本信息</div>
        <el-form label-width="120px">
            <el-form-item label="流程名称：">
                <span>{{ approveStore.approveMainModel.approveName }}</span>
            </el-form-item>
            <el-form-item label="发起人：">
                <span>{{ approveStore.approveMainModel.submitUserName }}</span>
            </el-form-item>
            <el-form-item label="发起时间：">
                <span>{{ commonUtil.Methods.formatDate(approveStore.approveMainModel.submitTime) }}</span>
            </el-form-item>
        </el-form>
        <div class="title-div" style="margin-top: 10px;">审批内容</div>
        <ProjectApprove v-if="approveStore.approveMainModel.flowTypeName == '立项审批'"
            :projectId="approveStore.approveMainModel.fId" ref="contentRef" />
        <RequirementsReview v-if="approveStore.approveMainModel.flowTypeName == '需求评审'"
            :requirementId="approveStore.approveMainModel.fId" ref="contentRef" />
        <SchedulesReview v-if="approveStore.approveMainModel.flowTypeName == '进度计划评审'"
            :scheduleId="approveStore.approveMainModel.fId" ref="contentRef" />
        <TeamReview v-if="approveStore.approveMainModel.flowTypeName == '团队资源审批'"
            :teamId="approveStore.approveMainModel.fId" ref="contentRef" />
        <template v-if="approveTable.checkedRecords.length > 0">
            <div class="title-div">审批记录</div>
            <el-form class="el-form-li" label-width="120px" v-for="r in approveTable.checkedRecords">
                <el-form-item label="审批人：">
                    <span>{{ r.approveUserName }}</span>
                </el-form-item>
                <el-form-item label="审批结果：">
                    <span>{{ TransferApproveResult(r.approveResult) }}</span>
                </el-form-item>
                <el-form-item label="审批意见：">
                    <span>{{ r.approveOpinion }}</span>
                </el-form-item>
            </el-form>
        </template>
    </div>
    <div v-if="receivedProps.showOperation" class="sjplatformAdd">
        <div class="title-div">审批意见</div>
        <el-form label-width="100px">
            <el-form-item label="是否同意：">
                <el-radio-group v-model="approveStore.approveRecordModel.approveResult">
                    <el-radio :value="1">同意</el-radio>
                    <el-radio :value="2">不同意</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="审批意见：">
                <el-input v-model="approveStore.approveRecordModel.approveOpinion" type="textarea" />
            </el-form-item>
            <el-form-item v-if="approveTable.nextNode && approveTable.nextNode.approveType == 4" label="审批人：">
                <el-input v-model="approveUserNames" placeholder="点击选择下一步审批人"
                    @click="openUserSelectForm('选择审批人', false)" clearable @clear="clearpersons" />
            </el-form-item>
        </el-form>
    </div>

    <el-dialog v-model="isOpenUserSelectForm" :title="formTitle" width="50%" draggable :destroy-on-close="true">
        <UserSelect :initSelectedUser="initSelectedUser" />
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="isOpenUserSelectForm = false;">取消</el-button>
                <el-button type="primary" @click="confirmPerson">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, defineProps, defineExpose } from "vue";
import { useApproveTable } from "../../hooks/ApproveManager/useApproveTable";
import ProjectApprove from "../ProjectManager/Common/ProjectApprove.vue";
import RequirementsReview from "../ProjectManager/Common/RequirementsReview.vue";
import SchedulesReview from "../ProjectManager/Common/SchedulesReview.vue";
import TeamReview from "../ProjectManager/Common/TeamReview.vue";
import UserSelect from "../../components/SystemManager/User/UserSelect.vue";
import commonUtil from "../../utils/common";
import { StatusEnum } from "../../hooks/Common/useStatusEnum";
import emitter from "../../utils/emitter";

let receivedProps = defineProps(['showOperation']);

//审批内容组件对象
let contentRef = ref(null);
let { approveTable, approveStore, doApprove, getAllCheckedRecordsByPId, updateApproveSpecificContent, getNextFlowNode } = useApproveTable(1, 10);
//审批记录
getAllCheckedRecordsByPId(approveStore.approveMainModel.id);
//下一个审批节点
getNextFlowNode(approveStore.approveMainModel.id,
    approveStore.approveRecordModel.flowId,
    approveStore.approveRecordModel.nodeId);


async function submitApprove() {
    await doApprove().then(async (response) => {
        //审批不通过
        if (approveStore.approveRecordModel.approveResult == 2) {
            approveStore.approveMainModel.status = StatusEnum.ApproveRefuse;
        }
        //审批通过
        if (approveStore.approveRecordModel.approveResult == 1 && response == 1) {
            //判断有没有下一个节点或当前节点是否所有审批人都已审批
            approveStore.approveMainModel.status = StatusEnum.ApprovePass;
        }

        //审批通过，但审批未结束 
        if (approveStore.approveRecordModel.approveResult == 1 && response == 0) {
            //判断有没有下一个节点或当前节点是否所有审批人都已审批
            approveStore.approveMainModel.status = StatusEnum.Approvaling;
        }

        //审批流程状态更新
        if (!approveStore.approveMainModel.updateProperties) {
            approveStore.approveMainModel.updateProperties = [];
        }
        approveStore.approveMainModel.updateProperties.push('Status');
        await updateApproveSpecificContent();
    })
    //更新状态
    contentRef.value.updateApproveStatus(approveStore.approveMainModel.status);
}

/*--------------------选择负责人开始--------------------*/
let approveUserNames = ref('');
let approveUserIds = ref('');
let approveUsers = ref([]);

let isOpenUserSelectForm = ref(false); //是否打开用户选择窗口
let formTitle = ref("");
let initSelectedUser = reactive({
    single: false,
    selectedrow: [],
});
//打开人员选择窗口
function openUserSelectForm(title, value) {
    formTitle.value = title;
    initSelectedUser.single = value;
    initSelectedUser.selectedrow = approveUserIds.value.split(',');
    isOpenUserSelectForm.value = true;
}
//清理负责人
function clearpersons() {
    approveUserNames.value = '';
    approveUserIds.value = '';
    approveUsers.value = [];
}
//确认选择的负责人
function confirmPerson() {
    emitter.emit("confirmSelectedUser");
}
//供用户选择调用设置选择的项
emitter.on("set-selected-user", (SelectionRows: any) => {
    let selectedUserId = [];
    let selectedUserName = [];
    approveUsers.value = [];
    approveStore.approveRecordModel.nextApproveRecords = [];
    clearpersons();
    for (let item of SelectionRows) {
        selectedUserId.push(item.id);
        selectedUserName.push(item.realName);
        approveUsers.value.push({ userId: item.id, userName: item.realName });

        approveStore.approveRecordModel.nextApproveRecords.push({
            flowId: approveStore.approveMainModel.flowId,
            nodeId: approveStore.approveRecordModel.nodeId,
            approveUserId: item.id,
            approveUserName: item.realName
        });
    }
    approveUserNames.value = selectedUserName.join(",");
    approveUserIds.value = selectedUserId.join(",");

    isOpenUserSelectForm.value = false;
});
/*--------------------选择负责人结束--------------------*/

function TransferApproveResult(value) {
    switch (value) {
        case 1:
            return '同意';
        case 2:
            return '不同意';
    }
    return '';
}

defineExpose({ submitApprove });
</script>